package com.richman.view;

import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.HGEvent;

public class HGPS38TeamMatchPane extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1111111111L;
	private JTextField textField;
	
	
	private String searchText = "";
	
	
	private  Vector<String[]> alreadyMergeTeams = new Vector<String[]>();
	
	
	private Vector<String[]> alreadyMergeTeamsShowItems = new Vector<String[]>();
	
	
	
	private Vector<String[]> HGTeams = new Vector<String[]>();
	
	private Vector<String[]> PS38Teams = new Vector<String[]>();
	
	
	AlreadyMergeTableModel alreadymergetablemodel = new AlreadyMergeTableModel();
    JTable alreadymergetable = null;
	
    
    //hg tables
    HGTeamsTableModel hgteamstablemodel = new HGTeamsTableModel();
    JTable hgteamstable = null;
    private JPopupMenu m_hgtablepopupMenu;
    private String[] hgSelectedItem = null;
    private String[] hgChooseItem = null;
    
    JMenuItem hgchooseMenuItem = null;
    JMenuItem hgmergeMenuItem = null;
    
    
    
    //ps38 tables
    PS38TeamsTableModel ps38teamstablemodel = new PS38TeamsTableModel();
    JTable ps38teamstable = null;
    private JPopupMenu m_ps38tablepopupMenu;
    private String[] ps38SelectedItem = null;
    private String[] ps38ChooseItem = null;
    
    JMenuItem ps38chooseMenuItem = null;
    JMenuItem ps38mergeMenuItem = null;
    

	/**
	 * Create the panel.
	 */
	public HGPS38TeamMatchPane() {
		setLayout(null);
		
		JLabel label = new JLabel("已合并球队：");
		label.setBounds(22, 35, 99, 21);
		add(label);
		
		
		
		
	    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
        tcr.setHorizontalAlignment(JLabel.CENTER);
		
		alreadymergetable = new JTable(alreadymergetablemodel);
		
		
		alreadymergetable.setRowHeight(30);
		alreadymergetable.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 13));

		
        alreadymergetable.setDefaultRenderer(Object.class, tcr);
        
		alreadymergetable.addMouseListener(new MouseAdapter(){//设置鼠标点击响应
            public void mouseClicked(MouseEvent e){
            	

            	
            	int columnIndex = alreadymergetable.columnAtPoint(e.getPoint()); //获取点击的列
                int rowIndex = alreadymergetable.rowAtPoint(e.getPoint()); //获取点击的行
                
                
                
                if(columnIndex == 2) { //删除
                	int n = JOptionPane.showConfirmDialog(null, "确认删除吗?", "删除账号", JOptionPane.YES_NO_OPTION);
    				if (n == JOptionPane.YES_OPTION) {

    					
    					
    					String[] deleteItem = alreadyMergeTeamsShowItems.elementAt(rowIndex); 
    					
    					for(int i = 0; i < alreadyMergeTeams.size(); i++) {
    						if(alreadyMergeTeams.elementAt(i)[0].equals(deleteItem[0]) && alreadyMergeTeams.elementAt(i)[1].equals(deleteItem[1])) {
    							
    							alreadyMergeTeams.remove(i);
    							break;
    						}
    					}
    					
    					alreadyMergeTeamsShowItems.remove(rowIndex);
    					
	            		updateHGPS38TeamsList();
    				}
                }
                

	               	
            }
        });

		JScrollPane scrollPane = new JScrollPane(alreadymergetable);
		scrollPane.setBounds(22, 97, 255, 426);
		add(scrollPane);
		
		JLabel label_1 = new JLabel("搜索：");
		label_1.setBounds(22, 66, 54, 15);
		add(label_1);
		
		textField = new JTextField();
		textField.setBounds(84, 66, 99, 21);
		add(textField);
		textField.setColumns(10);
		
		
		
		textField.addKeyListener(new KeyListener(){
            public void keyPressed(KeyEvent e) {  
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {  
                	searchText = textField.getText();  
                    
                    updateShowItem();
                    
                }  
                // System.out.println("Text " + value);  
            }  
            public void keyReleased(KeyEvent e) {  
            }  
            public void keyTyped(KeyEvent e) {  
            }  

        });
		
		
		
		
		
		
		
		
		JLabel label_2 = new JLabel("待合并场次：");
		label_2.setBounds(290, 38, 121, 15);
		add(label_2);
		
		JLabel lblSb = new JLabel("SB：");
		lblSb.setBounds(290, 66, 54, 15);
		add(lblSb);
		
		
		
		
		
		
		
		
		//hg teams table
		
		hgteamstable = new JTable(hgteamstablemodel);

		hgteamstable.setRowHeight(30);
		hgteamstable.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 13));

		hgteamstable.setDefaultRenderer(Object.class, tcr);
		
		m_hgtablepopupMenu = new JPopupMenu();
		hgchooseMenuItem = new JMenuItem();
		hgchooseMenuItem.setText("选择");
		hgchooseMenuItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事  
            	
            	try{
            		hgChooseItem = hgSelectedItem;
            		
            		
            	}catch(Exception e){
            		e.printStackTrace();
            	}

            }  
        });
		
		hgmergeMenuItem = new JMenuItem();
		hgmergeMenuItem.setText("合并PS38");
		hgmergeMenuItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事  
            	
            	try{
            		
            		hgChooseItem = hgSelectedItem;
            		
            		int n = JOptionPane.showConfirmDialog(null, "SB:" + Arrays.toString(hgChooseItem) + "\n" + "PS38:" + Arrays.toString(ps38ChooseItem) , "是否合并比赛", JOptionPane.YES_NO_OPTION);
            		
            		if (n == JOptionPane.YES_OPTION) {

    					String[] hgteams = hgChooseItem[2].split("-vs-");
    					String[] ps38teams = ps38ChooseItem[2].split("-vs-");
    					
    					boolean mergeFailed = false;
    					
    					boolean firstAlreadyin = false;
    					boolean secondAlreadyin = false;
    					
    					for(int i = 0; i < alreadyMergeTeams.size(); i++) {
    						if(hgteams[0].equals(alreadyMergeTeams.elementAt(i)[0])) {
    							if(!ps38teams[0].equals(alreadyMergeTeams.elementAt(i)[1])) {
    								mergeFailed = true;
    								break;
    							}else {
    								firstAlreadyin = true;
    							}
    						}
    						
    						if(hgteams[1].equals(alreadyMergeTeams.elementAt(i)[0])) {
    							if(!ps38teams[1].equals(alreadyMergeTeams.elementAt(i)[1])) {
    								mergeFailed = true;
    								break;
    							}else {
    								secondAlreadyin = true;
    							}
    						}
    						
    						
    					}
    					
    					
    					if(mergeFailed == true) {
    						JOptionPane.showMessageDialog(null, "球队已进行合并，请先删除之前的合并", "合并失败", JOptionPane.ERROR_MESSAGE);
    					}else {
    						
    						String[] newItem = {hgteams[0], ps38teams[0], "删除"};
    						if(firstAlreadyin == false) {
    							alreadyMergeTeams.add(newItem);
    						}
    						
    						
    						String[] newItem1 = {hgteams[1], ps38teams[1], "删除"};
    						if(secondAlreadyin==false) {
    							alreadyMergeTeams.add(newItem1);
    						}
    						
    						
    						//alreadymergetablemodel.updateTable();
    						
    						updateHGPS38TeamsList();
    						
    						JOptionPane.showMessageDialog(null, "合并成功", "合并成功", JOptionPane.DEFAULT_OPTION);
    						
    						
    						
    						
    						
    						
    						
    					}
    					
    					
    				}
            		
            		
            		
            	}catch(Exception e){
            		e.printStackTrace();
            	}

            }  
        });
		
		m_hgtablepopupMenu.add(hgchooseMenuItem);
		m_hgtablepopupMenu.add(hgmergeMenuItem);
		
		
		
		
		hgteamstable.addMouseListener(new java.awt.event.MouseAdapter() {  
            public void mouseClicked(java.awt.event.MouseEvent evt) {  
     	       //判断是否为鼠标的BUTTON3按钮，BUTTON3为鼠标右键  
     	       if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {  
     	          
     	           //弹出菜单  
     	         int selectedRow = hgteamstable.rowAtPoint(evt.getPoint());
     	           
     	         if(selectedRow == -1) {
     	        	 return;
     	         }
     	         
     	        String[] tmp = {HGTeams.elementAt(selectedRow)[0], HGTeams.elementAt(selectedRow)[1], HGTeams.elementAt(selectedRow)[2]};
     	        hgSelectedItem = tmp;
     	        
     	        
     	        if(ps38ChooseItem== null) {
     	        	hgmergeMenuItem.setEnabled(false);
     	        }else {
     	        	hgmergeMenuItem.setEnabled(true);
     	        }
     	        
     	           
     	    	m_hgtablepopupMenu.show(hgteamstable, evt.getX(), evt.getY());  
     	       } 
            }  
        });
		
		
		JScrollPane scrollPane_1 = new JScrollPane(hgteamstable);
		scrollPane_1.setBounds(300, 97, 564, 426);
		add(scrollPane_1);
		
		JLabel lblPs = new JLabel("PS38：");
		lblPs.setBounds(869, 66, 54, 15);
		add(lblPs);
		
		
		
		
		
		
		//ps38 teams
		
		ps38teamstable = new JTable(ps38teamstablemodel);
		
		
		ps38teamstable.setRowHeight(30);
		ps38teamstable.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 13));

		
		ps38teamstable.setDefaultRenderer(Object.class, tcr);
		
		
		
		
		m_ps38tablepopupMenu = new JPopupMenu();
		ps38chooseMenuItem = new JMenuItem();
		ps38chooseMenuItem.setText("选择");
		ps38chooseMenuItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事  
            	
            	try{
            		ps38ChooseItem = ps38SelectedItem;
            		
            		
            	}catch(Exception e){
            		e.printStackTrace();
            	}

            }  
        });
		
		ps38mergeMenuItem = new JMenuItem();
		ps38mergeMenuItem.setText("合并SB");
		ps38mergeMenuItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事  
            	
            	try{
            		
            		
            		ps38ChooseItem = ps38SelectedItem;
            		
            		
            		int n = JOptionPane.showConfirmDialog(null, "SB:" + Arrays.toString(hgChooseItem) + "\n" + "PS38:" + Arrays.toString(ps38ChooseItem) , "是否合并比赛", JOptionPane.YES_NO_OPTION);
           		
            		if (n == JOptionPane.YES_OPTION) {

    					String[] hgteams = hgChooseItem[2].split("-vs-");
    					String[] ps38teams = ps38ChooseItem[2].split("-vs-");
    					
    					boolean mergeFailed = false;
    					
    					for(int i = 0; i < alreadyMergeTeams.size(); i++) {
    						if(hgteams[0].equals(alreadyMergeTeams.elementAt(i)[0])) {
    							if(!ps38teams[0].equals(alreadyMergeTeams.elementAt(i)[1])) {
    								mergeFailed = true;
    								break;
    							}
    						}
    						
    						if(hgteams[1].equals(alreadyMergeTeams.elementAt(i)[0])) {
    							if(!ps38teams[1].equals(alreadyMergeTeams.elementAt(i)[1])) {
    								mergeFailed = true;
    								break;
    							}
    						}
    						
    						
    					}
    					
    					
    					if(mergeFailed == true) {
    						JOptionPane.showMessageDialog(null, "球队已进行合并，请先删除之前的合并", "合并失败", JOptionPane.ERROR_MESSAGE);
    					}else {
    						
    						String[] newItem = {hgteams[0], ps38teams[0], "删除"};
    						
    						alreadyMergeTeams.add(newItem);
    						
    						String[] newItem1 = {hgteams[1], ps38teams[1], "删除"};
    						
    						alreadyMergeTeams.add(newItem1);
    						
    						//alreadymergetablemodel.updateTable();
    						
    						updateHGPS38TeamsList();
    						
    						JOptionPane.showMessageDialog(null, "合并成功", "合并成功", JOptionPane.DEFAULT_OPTION);
    						

    					}
    					
    					
    				}
            		
            	}catch(Exception e){
            		e.printStackTrace();
            	}

            }  
        });
		
		m_ps38tablepopupMenu.add(ps38chooseMenuItem);
		m_ps38tablepopupMenu.add(ps38mergeMenuItem);
		
		
		
		
		ps38teamstable.addMouseListener(new java.awt.event.MouseAdapter() {  
            public void mouseClicked(java.awt.event.MouseEvent evt) {  
     	       //判断是否为鼠标的BUTTON3按钮，BUTTON3为鼠标右键  
     	       if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {  
     	          
     	           //弹出菜单  
     	         int selectedRow = ps38teamstable.rowAtPoint(evt.getPoint());
     	           
     	         if(selectedRow == -1) {
     	        	 return;
     	         }
     	         
     	        String[] tmp = {PS38Teams.elementAt(selectedRow)[0], PS38Teams.elementAt(selectedRow)[1], PS38Teams.elementAt(selectedRow)[2]};
     	        ps38SelectedItem = tmp;
     	        
     	        
     	        if(hgChooseItem== null) {
     	        	ps38mergeMenuItem.setEnabled(false);
     	        }else {
     	        	ps38mergeMenuItem.setEnabled(true);
     	        }
     	        
     	           
     	    	m_ps38tablepopupMenu.show(ps38teamstable, evt.getX(), evt.getY());  
     	       } 
            }  
        });
		
		
		
		
		
		
		
		
		JScrollPane scrollPane_2 = new JScrollPane(ps38teamstable);
		scrollPane_2.setBounds(874, 97, 583, 426);
		add(scrollPane_2);

		readHGPS38TeamsList();
		
		
	}
	
	
	//already merge teams
	
	public void updateHGPS38TeamsList() {		
		try{	
			File file = new File("data/" + "HGPS38Teams.txt");

			if(!file.exists()){	
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));

			String str = "";
			for(String [] obs : alreadyMergeTeams) {
				str = obs[0] + "," + obs[1];
				fwlocal.append(str);
				fwlocal.newLine();
				fwlocal.flush();
			}
			
			

			fwlocal.close();
			
			updateShowItem();
			
	
		}catch(FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch(IOException e) {
			e.printStackTrace();
			return;
		}
	}
	
	
	
	private void readHGPS38TeamsList() {
		File file = new File("data/" + "HGPS38Teams.txt");
		
		if(!file.exists()) {
			System.out.println("[HGPS38Teams] 文件不存在");
			return;
		}
		
		BufferedReader freader = null;
		try {
			freader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			
			String str = "";
			while ((str = freader.readLine()) != null) {
				String[] contents = str.split(",");
				if(contents.length == 2){
					String [] mergeteam = new String[3];
					mergeteam[0] = contents[0];
					mergeteam[1] = contents[1];

					mergeteam[2] = "删除";

					alreadyMergeTeams.add(mergeteam);
	        
				}
			}
			
			freader.close();
			
			updateShowItem();
			
			
			
		} catch(FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch(IOException e) {
			e.printStackTrace();
			return;
		}
					
	}
	
	
	private void updateShowItem() {
		
		alreadyMergeTeamsShowItems.clear();
		
		for(int i = 0; i < alreadyMergeTeams.size(); i++) {
			if(alreadyMergeTeams.elementAt(i)[0].contains(searchText) || alreadyMergeTeams.elementAt(i)[0].contains(searchText)) {
				alreadyMergeTeamsShowItems.add(alreadyMergeTeams.elementAt(i));
			}
		}
		
		
		alreadymergetablemodel.updateTable();
		
	}


	public Vector<String[]> getHGPS38TeamsList(){
		return alreadyMergeTeams;
	}
	
	
	
	
	
	
    public class AlreadyMergeTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 100861L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        { "SB", "PS38", "删除匹配"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public AlreadyMergeTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == alreadyMergeTeamsShowItems){
	    		return 0;
	    	}
            return alreadyMergeTeamsShowItems.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return alreadyMergeTeamsShowItems.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	

        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
        	alreadyMergeTeamsShowItems.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
	
	
    
    //hg teams
    
    public void updateHGTeams(Vector<HGEvent> hgevents) {
    	
    	
    	HGTeams.clear();
    	
    	Vector<String> alreadymergehgteams = new Vector<String>();
    	
    	
    	for(int i = 0; i < alreadyMergeTeams.size(); i++) {
    	
    		alreadymergehgteams.add(alreadyMergeTeams.elementAt(i)[0]);
    	}
    	
    	for(int i = 0; i < hgevents.size(); i++) {
    		
    		String[] teams = {hgevents.elementAt(i).homeTeam, hgevents.elementAt(i).awayTeam};
    		
    		if(alreadymergehgteams.contains(teams[0]) && alreadymergehgteams.contains(teams[1])) {
    			//System.out.println("merge already");
    		}else {
    			
    			String[] hgevent = {hgevents.elementAt(i).leagueName, hgevents.elementAt(i).eventTime, hgevents.elementAt(i).homeTeam + "-vs-" + hgevents.elementAt(i).awayTeam};
    			
    			HGTeams.add(hgevent);
    		}
    		
    	}
    	
    	
    	hgteamstablemodel.updateTable();
    	
    	
    }
	
    
    
    
    public class HGTeamsTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 100861L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        { "联赛", "时间", "球队"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public HGTeamsTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == HGTeams){
	    		return 0;
	    	}
            return HGTeams.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return HGTeams.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	

        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            HGTeams.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
    
    
    
    
 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //ps38 teams
    
    public void updatePS38Teams(Vector<Event> ps38Events) {
    	
    	PS38Teams.clear();
    	
    	
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
    	
    	Vector<String> alreadymergeps38teams = new Vector<String>();
    	
    	
    	for(int i = 0; i < alreadyMergeTeams.size(); i++) {
    	
    		alreadymergeps38teams.add(alreadyMergeTeams.elementAt(i)[1]);
    	}
    	
    	for(int i = 0; i < ps38Events.size(); i++) {
    		
    		String[] teams = {ps38Events.elementAt(i).homeTeam, ps38Events.elementAt(i).awayTeam};
    		
    		if(alreadymergeps38teams.contains(teams[0]) && alreadymergeps38teams.contains(teams[1])) {
    			System.out.println("merge already");
    		}else {
    			
    			String time = df.format(ps38Events.elementAt(i).eventTime);
    			
    			if(!ps38Events.elementAt(i).duringTime.equals("")) {
    				time = ps38Events.elementAt(i).duringTime;
    			}
    			
    			String[] ps38event = {ps38Events.elementAt(i).leagueName, time, ps38Events.elementAt(i).homeTeam + "-vs-" + ps38Events.elementAt(i).awayTeam};
    			
    			PS38Teams.add(ps38event);
    		}
    		
    	}
    	
    	ps38teamstablemodel.updateTable();
    	
    }
	
    
    
    
    public class PS38TeamsTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 100861L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        { "联赛", "时间", "球队"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public PS38TeamsTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == PS38Teams){
	    		return 0;
	    	}
            return PS38Teams.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return PS38Teams.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	

        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
        	PS38Teams.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
    
    
    
    
    
	
}
